//
//  PingLunTextView.m
//  TQY
//
//  Created by 甘世清 on 2024/7/12.
//

#import "PingLunTextView.h"

@implementation PingLunTextView

- (void)SQ_UIViewForSelfView{
    self.clipsToBounds = NO;
    UIView * bgView = [[UIView alloc]init];
    bgView.backgroundColor = UIColor.whiteColor;
    [self addSubview:bgView];

    
    [self addSubview:self.textView];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(14);
            make.width.mas_equalTo(SCREEN_WIDETH-80);
            make.height.mas_equalTo(36);
            make.bottom.equalTo(self.mas_bottom).offset(-10);
    }];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.top.equalTo(self.textView.mas_top).offset(-10);
    }];
    
    UIView * lineView = [[UIView alloc]init];
    lineView.backgroundColor = SQColorF1F1F1;
    [bgView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(bgView);
            make.height.mas_equalTo(1);
            make.top.equalTo(bgView);
    }];
    
    
    [self addSubview:self.fasongbtn];
    [self.fasongbtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_right);
            make.left.equalTo(self.textView.mas_right);
            make.bottom.equalTo(self.mas_bottom);
            make.height.mas_equalTo(56);
    }];
    
  
    
    [self.textView becomeFirstResponder];
    WS(weakSelf);
   
    [self.textView addTextDidChangeHandler:^(FSTextView *textView) {
        CGFloat  height = [textView sizeThatFits:CGSizeMake(SCREEN_WIDETH-80, MAXFLOAT)].height;
        if (height > 80) {
            height = 80;
        }  
        if ((height +20) > 56 ) {
            [UIView animateWithDuration:0.2 animations:^{
                [weakSelf.textView mas_updateConstraints:^(MASConstraintMaker *make) {
                                    make.height.mas_equalTo(height);
                }];
            }];
        }
    }];
    [self.fasongbtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        if ([weakSelf.textView.formatText isNotEmpty]) {
            if (weakSelf.textViewBlock) {
                [weakSelf endEditing:YES];
                weakSelf.textViewBlock(weakSelf.textView.formatText);
            } else {
                [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"操作失败")];
            }
        } else {
            [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"评论内容不能为空哦!")];
        }
    }];
}

- (FSTextView *)textView{
    if (!_textView) {
        _textView = [FSTextView textView];
        _textView.placeholder = SQNSLocalString(@"说点有趣的..");
        _textView.backgroundColor = SQColorF1F1F1;
        _textView.textColor = SQColor333333;
        _textView.placeholderColor = SQColor999999;
        _textView.placeholderFont = FONT_REGULAR(14);
        _textView.font = FONT_REGULAR(14);
        _textView.layer.cornerRadius = 6;
        _textView.layer.masksToBounds = YES;
    }
    return _textView;
}
- (SQCustomButton *)fasongbtn{
    if (!_fasongbtn) {
        _fasongbtn = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(0, 0) midmargin:0];
        _fasongbtn.text = SQNSLocalString(@"发送");
        _fasongbtn.textColor = SQColor3DA59D;
        _fasongbtn.font = FONT_MEDIUM(14);
    }
    return _fasongbtn;
}

@end
